package com.example.demoweb.framework;

import java.io.Serializable;

public class ExecuteResult<T> implements Serializable{
  private static final long serialVersionUID = 1L;
  private boolean success;
  private T data;
  private String error;

  public void setData(T data)
  {
    this.success = true;
    this.data = data;
  }

  public void setError(String error)
  {
    this.success = false;
    this.error = error;
  }

  public static <T> ExecuteResult<T> ok(T data)
  {
    ExecuteResult resp = new ExecuteResult();
    resp.setData(data);
    return resp;
  }

  public static <T> ExecuteResult<T> ok()
  {
    return ok(null);
  }

  public static <T> ExecuteResult<T> fail(String error)
  {
    ExecuteResult resp = new ExecuteResult();
    resp.setError(error);
    return resp;
  }

  public String toString()
  {
    return "Response(success=" + isSuccess() + ", data=" + getData() + ", error=" + getError() + ")";
  }

  public boolean isSuccess()
  {
    return this.success;
  }

  public void setSuccess(boolean success)
  {
    this.success = success;
  }

  public T getData()
  {
    return this.data;
  }

  public String getError()
  {
    return this.error;
  }
}
